package com.group15.phms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.group15.phms.AddNewDialogFragment.AddNewDialogListener;
import com.group15.phms.DeleteDialogFragment.DeleteDialogListener;

import android.os.Bundle;

import android.app.DialogFragment;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class DoctorInfo extends ListActivity implements DeleteDialogListener, AddNewDialogListener {
	SharedPreferences logins;
	DatabaseHelper db;
	SimpleAdapter adapter;
	String currentUser;
	ArrayList<Map<String, String>> data;
	ListView ItemList;
	List<Doctor> doctors;
	String deleteDoctorName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_doctor_info);
		// Show the Up button in the action bar.
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		logins = getSharedPreferences("logins",MODE_PRIVATE);
		currentUser = logins.getString("currentUser","INVALID");
		db= new DatabaseHelper(getApplicationContext());
		
		findViewById(R.id.personalInfoTable).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						userInfo();
					}
				});
		findViewById(R.id.contactInfoTable).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						contactInfo();
					}
				});
		
		doctors = db.getDoctors(currentUser);
		db.closeDB();
		
		data = new ArrayList<Map<String, String>>();
		for (Doctor d : doctors) {
		    Map<String, String> datum = new HashMap<String, String>(2);
		    datum.put("name", d.getName());
		    datum.put("email", d.getEmail());
		    data.add(datum);
		}
		
		
		adapter = new SimpleAdapter(this, data,
                                      R.layout.doctor_row,
                                      new String[] {"name", "email"},
                                      new int[] {R.id.toptext,
                                                 R.id.bottomtext});
		ListView itemList=(ListView)findViewById(android.R.id.list);
		itemList.setAdapter(adapter);
		itemList.setEmptyView(findViewById(R.id.empty));
		
		this.getListView().setLongClickable(true);
		this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
		    public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
		    	DialogFragment f = new DeleteDialogFragment();
		        f.show(getFragmentManager(), "DeleteDialogFragment");
		        deleteDoctorName = doctors.get(position).getName();
		        return true;
		    }
		});
	}

	
	@Override
	public void onRestart()
	{
		super.onRestart();
		db= new DatabaseHelper(getApplicationContext());
		doctors = db.getDoctors(currentUser);
		data = new ArrayList<Map<String, String>>();
		for (Doctor d : doctors) {
		    Map<String, String> datum = new HashMap<String, String>(2);
		    datum.put("name", d.getName());
		    datum.put("email", d.getEmail());
		    data.add(datum);
		}
		adapter = new SimpleAdapter(this, data,
                R.layout.doctor_row,
                new String[] {"name", "email"},
                new int[] {R.id.toptext,
                           R.id.bottomtext});
		ListView itemList=(ListView)findViewById(android.R.id.list);
		itemList.setAdapter(adapter);
		itemList.setEmptyView(findViewById(R.id.empty));
		adapter.notifyDataSetChanged();
		db.closeDB();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.doctor_info, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.action_logout:
			intent = new Intent(this, Login.class);
	    	startActivity(intent);
	    	finish();
			return true;
		case R.id.action_search:
			intent = new Intent(this, Search.class);
	    	startActivity(intent);
			return true;
		case R.id.editbutton:
			editFields();
			return true;
		case R.id.addbutton:
			DialogFragment f = new AddNewDialogFragment();
	        f.show(getFragmentManager(), "AddDoseDialogFragment");
            return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	//delete a doctor
	@Override
	public void onPositiveClick()
	{
		db= new DatabaseHelper(getApplicationContext());
		db.deleteDoctor(deleteDoctorName, currentUser);
		Toast.makeText(getApplicationContext(),"Doctor deleted successfully", Toast.LENGTH_SHORT).show();
		doctors = db.getDoctors(currentUser);
		data = new ArrayList<Map<String, String>>();
		for (Doctor d : doctors) {
		    Map<String, String> datum = new HashMap<String, String>(2);
		    datum.put("name", d.getName());
		    datum.put("email", d.getEmail());
		    data.add(datum);
		}
		adapter = new SimpleAdapter(this, data,
                R.layout.doctor_row,
                new String[] {"name", "email"},
                new int[] {R.id.toptext,
                           R.id.bottomtext});
		ListView itemList=(ListView)findViewById(android.R.id.list);
		itemList.setAdapter(adapter);
		itemList.setEmptyView(findViewById(R.id.empty));
		adapter.notifyDataSetChanged();
		db.closeDB();
	}
	
	//add new Doctor
	@Override
	public void onPositiveClick(View view) {
		db= new DatabaseHelper(getApplicationContext());
		String name = ((EditText) view.findViewById(R.id.name)).getText().toString();
 	    String email = ((EditText) view.findViewById(R.id.email)).getText().toString();
		Doctor doc = new Doctor(name, email);
		String exception = db.addDoctor(doc, currentUser);
		doctors = db.getDoctors(currentUser);
		data = new ArrayList<Map<String, String>>();
		for (Doctor d : doctors) {
		    Map<String, String> datum = new HashMap<String, String>(2);
		    datum.put("name", d.getName());
		    datum.put("email", d.getEmail());
		    data.add(datum);
		}
		adapter = new SimpleAdapter(this, data,
                R.layout.doctor_row,
                new String[] {"name", "email"},
                new int[] {R.id.toptext,
                           R.id.bottomtext});
		ListView itemList=(ListView)findViewById(android.R.id.list);
		itemList.setAdapter(adapter);
		itemList.setEmptyView(findViewById(R.id.empty));
		adapter.notifyDataSetChanged();
		db.closeDB();
		if (exception.equals("success"))
			Toast.makeText(getApplicationContext(),"Doctor added successfully", Toast.LENGTH_SHORT).show();
		else
			Toast.makeText(getApplicationContext(),"Doctor NOT added.\nName already exists.", Toast.LENGTH_LONG).show();
	}
	
	
	void editFields()
	{
		
	}
	void addNew()
	{
		
	}
	void userInfo()
	{
		Intent intent=new Intent(this, UserInfo.class);
		startActivity(intent);
		finish();
	}
	void contactInfo()
	{
		Intent intent=new Intent(this, ContactInfo.class);
		startActivity(intent);
		finish();
	}
}



